package com.example.finalassignment.controller;

import com.example.finalassignment.exception.ServiceException;
import com.example.finalassignment.mapper.TechnologyInfoMapper;
import com.example.finalassignment.mapper.TechnologyMapper;
import com.example.finalassignment.pojo.Technology;
import com.example.finalassignment.pojo.dto.TechnologyDto;
import com.example.finalassignment.pojo.response.ResultDataResponse;
import com.example.finalassignment.pojo.response.ResultResponse;
import com.example.finalassignment.service.TechnologyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

/**
 * 创作
 */

@RestController
@RequestMapping("/writes")
public class TechnologyController {

    private final TechnologyService technologyService;
    private final TechnologyMapper technologyMapper;
    private final TechnologyInfoMapper technologyInfoMapper;

    public TechnologyController(TechnologyService technologyService, TechnologyMapper technologyMapper, TechnologyInfoMapper technologyInfoMapper) {
        this.technologyService = technologyService;
        this.technologyMapper = technologyMapper;
        this.technologyInfoMapper = technologyInfoMapper;
    }

    @PostMapping("/editor")
    public ResultResponse editor(@RequestBody TechnologyDto technologyDto) {
        String title = technologyDto.getTitle();
        String content = technologyDto.getContent();
        String description = technologyDto.getDescription();
        String showPhoto = technologyDto.getShowPhoto();

        if (content == null || content.isEmpty()) {
            return ResultResponse.fail(201, "内容不能为空");
        }

        if (title == null || title.isEmpty()) {
            return ResultResponse.fail(201, "标题不能为空");
        }

        if (description == null || description.isEmpty()) {
            return ResultResponse.fail(201, "请写一段话用于描述文章");
        }

        if (showPhoto == null || showPhoto.isEmpty()) {
            return ResultResponse.fail(201, "请上传一张照片用于展示");
        }

        // 只存uuid
        int idx = showPhoto.lastIndexOf('/');
        showPhoto = showPhoto.substring(idx + 1);
        technologyDto.setShowPhoto(showPhoto);
        return technologyService.editor(technologyDto);
    }

    /**
     * 获取所有文章 根据创建时间降序
     */
    @GetMapping("/all/{value}")
    public ResultResponse getAllDataByLimit(@PathVariable("value") String value,Integer start, Integer pages) {
        if("75395123".equals(value)) {
            return technologyService.getAllDataByLimit(start, pages);
        }
        if(start == null || start <= 0){
            start = 1;
        }

        if(pages == null){
            pages = 5;
        }

        Integer finalStart = start;
        Integer finalPages = pages;
        CompletableFuture<List<Map<String, Object>>> getAllDataYesLimitCompletableFuture = CompletableFuture.supplyAsync(new Supplier<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> get() {
                return technologyMapper.getAllDataByLimitLike(value,(finalStart - 1) * finalPages, finalPages);
            }
        });

        List<Map<String, Object>> getAllDataYesLimit = getAllDataYesLimitCompletableFuture.join();

        List<Map<String, Object>> allDataByNoLimit = technologyMapper.getAllDataByNoLimitLike(value);

        Map<String,Object> map = new HashMap<>();
        map.put("total",allDataByNoLimit.size());
        getAllDataYesLimit.add(map);
        return new ResultDataResponse<>(getAllDataYesLimit);
    }
    /**
     * 分页查询userid的文章
     */
    @GetMapping("/userId")
    public ResultResponse getDataByLimit(Integer start, Integer pages) {
        return technologyService.getDataByLimit(start, pages);
    }

    /**
     * 根据uuid查询文章
     */
    @GetMapping("/{uuid}")
    public ResultResponse getDraftsByUUID(@PathVariable("uuid") String uuid) {
        return new ResultDataResponse<>(technologyMapper.getDraftsByUUID(uuid));
    }

    /**
     * 观看
     */
    @GetMapping("/watch/{uuid}")
    public ResultResponse updateDraftsByUUIDToWatch(@PathVariable("uuid") String uuid) {
        try {
            technologyInfoMapper.updateDraftsByUUIDToWatch(uuid);
            return ResultResponse.success("成功");
        }catch (Exception e){
            throw new ServiceException(201,"服务器开小差！");
        }
    }

    /**
     * 删除文章(软删除)
     */
    @DeleteMapping("/{uuid}")
    public ResultResponse deleteDraftsByUUID(@PathVariable("uuid") String uuid) {
        try {
            technologyMapper.deleteDraftsByUUID(uuid);
            return ResultResponse.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(201, "服务器异常");
        }
    }
}
